package com.ymatou.iapi.coupon.testcase;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.GetAllSellerBatchInfosBean;
import com.ymatou.iapi.coupon.parameter.batchInfo;
import com.ymatou.iapi.coupon.parameter.batchInfos;
import com.ymatou.iapi.coupon.service.GetAllSellerBatchInfosCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
/**
*获取所有商家可以领取的批次
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetAllSellerBatchInfos{
	private static GetAllSellerBatchInfosBean getallsellerbatchinfosBean;
	private static GetAllSellerBatchInfosCall getallsellerbatchinfosCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取所有商家可以领取的批次");
	}

	@Before
	public void caseUp() {
		getallsellerbatchinfosBean=new GetAllSellerBatchInfosBean();
		getallsellerbatchinfosCall=new GetAllSellerBatchInfosCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getallsellerbatchinfosCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAllSellerBatchInfos_001() {
		Logger.start(true, "获取所有买手优惠券集合");
        try {

            getallsellerbatchinfosCall.setData(getallsellerbatchinfosBean);
            getallsellerbatchinfosCall.callService();

            Logger.verifyEquals("0", getallsellerbatchinfosCall.getCode(),
                    "验证返回code");

            batchInfos sellerCouponValueList = getallsellerbatchinfosCall.getSellerCouponValueList();

            for (batchInfo sellerCouponValue : sellerCouponValueList.getBatchInfos()) {
                couponbatch batch = new
                        couponbatchWapper().selectByPrimaryKey(sellerCouponValue.getBatchId());
                Logger.verifyEquals(batch.getBatchCode(),
                        sellerCouponValue.getBatchCode(),
                        "验证返回BatchCode与数据库一致");

                Logger.verifyEquals(batch.getOperatorId(),
                        sellerCouponValue.getSellerId(),
                        "验证返回SellerId与数据库一致");

//                Logger.verifyEquals(batch.getiCouponSettingId(),
//                        sellerCouponValue.getCouponSettingId(),
//                        "验证返回CouponSettingId与数据库一致");

//                YmtCouponSetting setting =
//                        new
//                        YmtCouponSettingWapper().selectByiCouponSettingId(sellerCouponValue.getCouponSettingId())
//                                .get(0);

                // 验证ValidStart，ValidEnd
                Logger.verifyEquals(getDateString(batch.getValidStart()),
                        getDateString2(sellerCouponValue.getValidStart()),
                        "验证返回ValidStart与数据库表Ymt_CouponSetting中字段dValidStart一致");
                Logger.verifyEquals(getDateString(batch.getValidEnd()),
                        getDateString2(sellerCouponValue.getValidEnd()),
                        "验证返回ValidEnd与数据库表Ymt_CouponSetting中字段dValidEnd一致");

                Logger.verifyEquals(batch.getUseConditionType(),
                        sellerCouponValue.getCouponUseType(),
                        "验证返回CouponUseType与数据库一致");

                Logger.verifyEquals(batch.getReceiveTimesPerUser(),
                        sellerCouponValue.getMaxUseTimePerUser(),
                        "验证返回MaxUseTimePerUser与数据库一致");

//                YmtCouponValue couponValue =
//                        new YmtCouponValueWapper().selectByiCouponSettingId(sellerCouponValue.getCouponSettingId())
//                                .get(0);

                Logger.verifyEquals(0,
                        sellerCouponValue.getMinOrderValue().compareTo(batch.getMinOrderValue()),
                        "验证返回MinOrderValue与数据库一致");

                Logger.verifyEquals(0,
                        sellerCouponValue.getCouponValue().compareTo(batch.getCouponValue()),
                        "验证返回MinOrderValue与数据库一致");
            }

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    private String getDateString(Date date) {
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");

        return formatter1.format(date) + "T" + formatter2.format(date);
    }

    private String getDateString2(String date) {
        if (date.length() != 19) {
            date = date.substring(0, 19);
        }

        return date;
    }

    // 与数据库返回做验证，但是逻辑经常变，所以
    private void verifyAllReturnWithDB()
    {
        List<batchInfo> sellerCouponValueList = getallsellerbatchinfosCall
                .getSellerCouponValueList().getBatchInfos();

        List<Map> result = new couponbatchWapper()
                .getAllSellerCouponList();

        Logger.verifyEquals(result.size(), sellerCouponValueList.size(),
                "验证返回优惠券条数正确");

        for (int i = 0; i < sellerCouponValueList.size(); i++) {
            // for (int i = 0; i < 2; i++) {

            Logger.comment("验证第" + i + "条记录");

            couponbatch newresult =
                    new
                    couponbatchWapper().selectBybatchCode(sellerCouponValueList.get(i).getBatchCode()).get(
                            0);

            Logger.verifyEquals(newresult.getOperatorId(), sellerCouponValueList
                    .get(i).getSellerId(), "验证返回SellerId与数据库一致");
            Logger.verifyEquals(newresult.getBatchId(),
                    sellerCouponValueList.get(i).getBatchId(),
                    "验证返回BatchId与数据库一致");
//            Logger.verifyEquals(newresult.getCouponSettingId(),
//                    sellerCouponValueList.get(i).getCouponSettingId(),
//                    "验证返回CouponSettingId与数据库一致");

            Logger.verifyEquals(
                    0,
                    sellerCouponValueList
                            .get(i)
                            .getMinOrderValue()
                            .compareTo(
                                    new BigDecimal(result.get(i)
                                            .get("MinOrderValue")
                                            .toString())),
                    "验证返回MinOrderValue与数据库一致");
            Logger.verifyEquals(
                    0,
                    sellerCouponValueList
                            .get(i)
                            .getCouponValue()
                            .compareTo(
                                    new BigDecimal(result.get(i)
                                            .get("CouponValue").toString())),
                    "验证返回CouponValue与数据库一致");
            Logger.verifyEquals(Integer.parseInt(result.get(i)
                    .get("CouponUseType").toString()),
                    sellerCouponValueList.get(i).getCouponUseType(),
                    "验证返回CouponUseType与数据库一致");

            Logger.verifyEquals(
                    getDateString((Date) result.get(i).get("ValidStart")),
                    getDateString2(sellerCouponValueList.get(i)
                            .getValidStart()), "验证返回ValidStart与数据库一致");
            Logger.verifyEquals(
                    getDateString((Date) result.get(i).get("ValidEnd")),
                    getDateString2(sellerCouponValueList.get(i)
                            .getValidEnd()), "验证返回ValidEnd与数据库一致");

        }
    }

}